package com.wyg.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import com.wyg.dao.QuestionDao;
import com.wyg.pojo.Question;
import com.wyg.util.HibernateSessionFactory;

public class QuestionService {
	
	public HttpServletRequest request;
	public HttpServletResponse response;
	public QuestionDao rd = new QuestionDao();
	
	public QuestionService(HttpServletRequest request, HttpServletResponse response) {
		super();
		this.request = request;
		this.response = response;
	}
	
	//显示题目信息
	public void showQuestion(){
		List list = rd.showPartQuestion();
		request.setAttribute("questions", list);
		try {
			request.getRequestDispatcher("usermain/questions.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//题库管理显示题目信息
	public void showChoiceQuestion(){
		List list = rd.showAllChoiceQuestion();
		String who = request.getParameter("who");
		System.out.println(who);
		request.setAttribute("questions", list);
		try {
			if(who.equals("manager")){
				request.getRequestDispatcher("main/choiceQuestion.jsp").forward(request, response);
			}else{
				request.getRequestDispatcher("usermain/questions.jsp").forward(request, response);
			}

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//题库管理显示题目信息
		public void showJudgeQuestion(){
			List list = rd.showAllJudgeQuestion();
			String who = request.getParameter("who");
			request.setAttribute("questions", list);
			try {
				if(who.equals("manager")){
					request.getRequestDispatcher("main/judgeQuestion.jsp").forward(request, response);
				}else{
					request.getRequestDispatcher("usermain/questions.jsp").forward(request, response);
				}

			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	//跳转页面到修改题目页面
	public void goChangeQuestion(){
		int id = Integer.valueOf(request.getParameter("qid"));
	 	Question q = rd.showOneQuestion(id);
	 	request.setAttribute("question", q);
		try {
			request.getRequestDispatcher("main/changeQuestion.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//修改选择
	public void changeQuestion(){
		int id =Integer.valueOf(request.getParameter("id"));
		String title = request.getParameter("title");
		String optionA = request.getParameter("optionA");
		String optionB = request.getParameter("optionB");
		String optionC = request.getParameter("optionC");
		String optionD = request.getParameter("optionD");
		String answer = request.getParameter("answer");
		rd.changeChiceQuestion(id, title, optionA, optionB, optionC, optionD, answer);
		try {
			request.getRequestDispatcher("main/ok.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	

}
